package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.DoMainUtils;
import com.kdtech.utils.RegexUtils;

public class LoupanNewsAnalyse implements AnalyseNews {

	private static final String[] regex={
		"http://.*.loupan.com/loupan/[a-z]*/[a-z]*_[0-9]*.html",
		"http://.*.loupan.com/html/news/[0-9]{6}/[0-9]*.html"
	};

	
	public boolean isDetailPage(String url) {
		return RegexUtils.matchAnyIgnoreCase(url, regex);
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		if (urlMeta.getHtml() == null) {
		}
		String url=urlMeta.getUrl();
		if (!isDetailPage(url)) {
		}
//		NewsMeta news=new ParseNewsUtils().parse(urlMeta);
		NewsMeta news=new NewsMeta();
		String htmltxt=urlMeta.getHtml();
		Document doc=Jsoup.parse(htmltxt);
		String title=null;
		String content=null;
		String author=null;
		Long date=null;
		title=doc.select("h1").text();
		if(StringUtils.isBlank(title)){
			title=doc.select("h3").text();
		}
		content=HtmlCleaner.getContentHtml(url,doc.select("div.cont,div.news-box"));
		if(StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select("div.lp-detail-content"));
			if(StringUtils.isBlank(content)){
				content=HtmlCleaner.getContentHtml(url,doc.select("ul.nb"));
				if(StringUtils.isBlank(content)){
					content=HtmlCleaner.getContentHtml(url,doc.select("div.new-text-p"));
				}
			}
		}

		/*
		 * 作者
		 */
		String authorStr=null;
		authorStr=doc.select("div.new_bt,.time-info").text();
		if(StringUtils.isBlank(authorStr)){
			authorStr=doc.select("div.lp-detail-title").text();
			if(StringUtils.isBlank(authorStr)){
				authorStr=doc.select("p.time").text();
			}
		}
		author=JSoupUtils.matchAuthor(doc, "来源:");

		date=DateUtils.matchDate(doc.select("div.new_bt,.time-info").text());
		if(date == null){
			date=DateUtils.matchDate(doc.select("div.lp-detail-title").text());
			if(date == null){
				date=DateUtils.matchDate(doc.select("p.time").text());
				if(date == null){
					date=DateUtils.matchDate(doc.select("p.time-form").text());
				}
			}
		}
		news.setUrl(url);
		news.setTitle(title);
		news.setContent(content);
		news.setAuthor(author);
		news.setDate(date);
		if (StringUtils.isBlank(news.getTitle())||news.getDate() == null) {
		}
		return news;
	}

	public static void main(String[] args) {
		String url="http://sz.loupan.com/html/news/201502/1592344.html";
		LoupanNewsAnalyse test=new LoupanNewsAnalyse();
		url=UrlArgumentTop.FromatUrl(url);
		System.out.println(test.isDetailPage(url));
		System.out.println(url);
		System.out.println(DoMainUtils.GetDomainName(url));
		UrlMeta meta=CrawlHTML.responseToURL(url);
		NewsMeta parserHtml=test.parserHtml(meta);
		System.out.println(parserHtml);
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
